<template>
  <div class="ChildPage TablePage" v-loading="loading">
    <vb-page ref="page">
      <template #table-handle-prepend="scope">
        <el-button size="mini" type="text" icon="el-icon-edit" @click="gotoDetail(scope.row.billNo, scope.row.stockNo)">查看</el-button>
        <el-button size="mini" type="text" style="color:var(--el-color-success)" icon="el-icon-share" @click="toExport(scope.row.billNo, scope.row.stockNo)">导出</el-button>
      </template>
      <template #headerSearch-append="scope">
        <div>
          <span style="font-size: 12px; padding-right: 10px; color: #666666">物料</span>
          <el-select filterable remote :remote-method="e => getMaterialList(e)" v-model="scope.searchForm.materialNo" size="small" clearable class="material" placeholder="请输入物料编码或者名称" style="margin-right: 10px">
            <el-option v-for="item in materialList" :key="item.fNumber" :label="item.fNumber + '：' + item.fName + ' ' + item.fSpecification" :value="item.fNumber" />
          </el-select>
        </div>
        <el-space>
          <span style="font-size: 12px; color: #666666">批次</span>
          <el-input size="small" v-model="scope.searchForm.batchNo" placeholder="请输入批次号" clearable></el-input>
        </el-space>
        <el-space>
          <span style="font-size: 12px; color: #666666">箱号</span>
          <el-input size="small" v-model="scope.searchForm.boxNo" placeholder="请输入箱号" clearable></el-input>
        </el-space>
      </template>
      <template #headerSearch-btn-append="{ searchForm }">
        <el-button type="warning" size="small" icon="el-icon-download" @click="doDownload(searchForm)">导出其他入库详情</el-button>

      </template>
      <template #table-stockNo="scope">
        {{ getStockName(scope.row.stockNo) }}
      </template>
      <template #table-isPrint="scope">
        <el-tag :type="scope.row.isPrint? 'success': 'info'">{{ scope.row.isPrint? '已打印': '未打印' }}</el-tag>
      </template>
      <template #table-count="scope">
        {{ scope.row.count? Number(scope.row.count.toFixed(6)): scope.row.count }}
      </template>
      <template #table-status="scope">
        <el-tag :type="scope.row.status === 'save'? 'info': 'success'">{{ scope.row.status === 'save'? '保存': '审核' }}</el-tag>
      </template>
    </vb-page>
  </div>
</template>

<script>
export default {
  data() {
    return {
      materialLoading: false,
      reloadMaterial: false,
      materialList: [],
    }
  },
  async mounted() {
    await this.getStock()
  },
  methods: {
    async doDownload(searchForm) {
      this.loading = true
      try {
        let res = await this.callApi('download_otherIn_detail', searchForm)
        if (!res.res) return
      } finally {
        this.loading = false
      }
    },
    async getMaterialList(val) {
      if (!val) {
        this.materialList = []
        return
      }
      this.materialLoading = true
      try {
        const res = await this.callApi('get_material_by_key', { key: val, pageSize: 20 })
        if (!res.res) return
        this.materialList = res.data
      } finally {
        this.materialLoading = false
      }
    },
    toExport(billNo,stockNo) {
      window.open(import.meta.env.VITE_EXPORT_URL + `otherExport?billNo=${ billNo}&stockNo=${stockNo}`)
    },
    async getStock() {
      this.loading = true;
      try {
        let res = await this.$api.getChildDictionaryListByKeys({
          keys: ['stock']
        });
        if (!res.res) return;
        this.stockList = res.stock;
      } finally {
        this.loading = false;
      }
    },
    /**
     * 获取状态名
     */
    getStockName(val) {
      if (this.stockList && this.stockList.length > 0) {
        for(let i = 0; i < this.stockList.length; i++) {
          if (this.stockList[i].key === val) {
            return this.stockList[i].name;
          }
        }
      }
      return '';
    },
    gotoDetail(billNo = '', stockNo = '') {
      let url = '/other/otherInDetail'
      if (billNo && stockNo) {
        url += `?billNo=${ billNo }&stockNo=${stockNo}`
      }
      this.$router.push(url)
    }
  }
}
</script>

<style scoped>

</style>
